import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
// import { whiteList } from '@/config/whiteList.js'
import localCache from '@/utils/cache'
// import NProgress from '@/utils/nprogress'
// RouteRecordRaw 内置的类型接口,也可以使用自定义的类型约束: IRouterList[]或者Array<IRouterList>
// const routes = [
const routes: Array<RouteRecordRaw> = [
  // {
  //   path: '/layout',
  //   name: 'layout',
  //   component: () => import('../view/main/user/user.vue'),
  //   meta: {}
  //   // children:[]    //根据用户登录时获取的菜单来决定所拥有的子集
  // }
  // {
  //   path: '/textrouter/:title',
  //   name: 'textrouter',
  //   component: () => import('../pages/textRouter/index.vue')
  // }
  // ,
  {
    path: '/ws',
    name: 'websocket',
    component: () => import('../view/main/ws/ws.vue')
  },
  {
    path: '/hooks',
    name: 'hooks',
    component: () => import('../view/main/hooks.vue')
  }
]

let menus = [
  {
    children: [
      {
        children: [
          {
            id: 41,
            parentId: 39, //对应父级id
            name: '三级',
            sort: 106,
            type: 3,
            url: '/main/analysis/userview',
            permission: 'system:user:create' //按钮的权限
          }
        ],
        id: 39,
        parentId: 38, //对应父级id
        name: '核心技术',
        sort: 106,
        type: 2,
        url: '/main/analysis/overview'
      }
    ],
    icon: 'el-icon-monitor',
    id: 38, //父级id
    name: '系统总览',
    sort: 1,
    type: 1,
    url: '/main/analysis'
  },
  {
    children: [],
    icon: 'el-icon-monitor',
    id: 40,
    name: '系统总览',
    sort: 1,
    type: 1,
    url: '/main/analysis'
  }
]

let data = [
  {
    id: '1b8651cd-2a22-4202-a236-01747a2ff404',
    parentId: '0',
    children: [
      {
        id: '02ddeeb8-a496-4200-9d9e-233b0012b2f5',
        parentId: '1b8651cd-2a22-4202-a236-01747a2ff404',
        children: null,
        name: '统计报表',
        code: '20241205071244030033',
        status: 1,
        type: 0,
        level: 2,
        appId: 'a56fb332-fd68-4be2-acd7-e2ea9f7aac76',
        appName: '标注系统',
        dimId: '',
        dimName: null,
        urlType: 0,
        url: '/statist',
        sort: null,
        remark: '',
        createTime: '2024-12-05 15:12:45',
        updateTime: '2024-12-05 15:14:13',
        ico: ''
      },
      {
        id: 'aede768a-ec43-4b32-8c88-26790ff24f93',
        parentId: '1b8651cd-2a22-4202-a236-01747a2ff404',
        children: null,
        name: '任务管理',
        code: '20241205031700030011',
        status: 1,
        type: 0,
        level: 2,
        appId: 'a56fb332-fd68-4be2-acd7-e2ea9f7aac76',
        appName: '标注系统',
        dimId: '',
        dimName: null,
        urlType: 0,
        url: '/task',
        sort: null,
        remark: '',
        createTime: '2024-12-05 11:17:01',
        updateTime: '2024-12-05 11:28:19',
        ico: ''
      },
      {
        id: '4757935b-1c13-494b-8994-23348b028233',
        parentId: '1b8651cd-2a22-4202-a236-01747a2ff404',
        children: [
          {
            id: '69a7aaa9-23f4-425a-86fe-50337a3330e3',
            parentId: '4757935b-1c13-494b-8994-23348b028233',
            children: null,
            name: '公会成员管理',
            code: '20241217013355030037',
            status: 1,
            type: 0,
            level: 3,
            appId: 'a56fb332-fd68-4be2-acd7-e2ea9f7aac76',
            appName: '标注系统',
            dimId: '',
            dimName: null,
            urlType: 0,
            url: '/guildMembers',
            sort: null,
            remark: '',
            createTime: '2024-12-17 09:33:56',
            updateTime: '2024-12-17 09:35:38',
            ico: ''
          }
        ],
        name: '公会管理',
        code: '20241205031734030012',
        status: 1,
        type: 0,
        level: 2,
        appId: 'a56fb332-fd68-4be2-acd7-e2ea9f7aac76',
        appName: '标注系统',
        dimId: '',
        dimName: null,
        urlType: 0,
        url: '/guild',
        sort: null,
        remark: '',
        createTime: '2024-12-05 11:17:34',
        updateTime: '2024-12-05 11:27:53',
        ico: ''
      }
    ],
    name: '标注系统',
    code: '20241205031630030010',
    status: 1,
    type: 0,
    level: 1,
    appId: 'a56fb332-fd68-4be2-acd7-e2ea9f7aac76',
    appName: '标注系统',
    dimId: null,
    dimName: null,
    urlType: 0,
    url: null,
    sort: null,
    remark: null,
    createTime: '2024-12-05 11:16:31',
    updateTime: '2024-12-05 11:16:31',
    ico: null
  }
]

const cRouter = () => {
  return createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHistory(),
  routes
})
}

const router = cRouter()

export function resetRouter() {
  const newRouter = cRouter()
  router.matcher = newRouter.matcher // reset router // 重新设置路由的可匹配路径  matcher就是原来静态路由里面的数据
}

// router.beforeEach(async (to, from, next) => {
//   NProgress.start()
//   const token = localCache.getCache('token', true)
//   // 白名单
//   if (whiteList.includes(to.path)) {
//     next()
//     return
//   }
//   if (!token) {
//     if(to.path === '/home'){
//       return next()
//     }
//     if(to.path === '/login'){
//       return
//     }
//     // 缓存现在页面
//     next(`/login?redirection=${encodeURIComponent(`${location.href}`)}`)
//   } else {
//     const store = useLoginStore()
//     if (!store.menus?.length) {
//       // 其他页面直接进入 存储路由
//       const data = await getUserMenuList()
//       store.setMenus(data)
//     }
//     // 如果页面是基础模板路径则调到第一个子路径
//     if (to.path === '/main' && store.menus.length) {
//       const list = store.menus?.find((a) => a.url == to.path)
//       const url = list?.children[0]?.children[0]?.url
//       if (url) {
//         next(url)
//         return
//       }
//     }
//     next()
//   }
// })

// router.afterEach(() => {
//   NProgress.done()
// })

export default router
